package com.xxxx.portal.config;

import com.xxxx.portal.interceptor.CommonInterceptor;
import com.xxxx.portal.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * @Author GP
 * @Date 2020/8/31 21:16
 */
@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {

    @Resource
    private LoginInterceptor loginInterceptor;

    @Resource
    private CommonInterceptor commonInterceptor;

    /**
     * 添加拦截器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //registry.addInterceptor(loginInterceptor)   //自定义拦截器
        //.addPathPatterns("/**")     //添加拦截请求
        //.excludePathPatterns("/**")
        //TODO 需拦截购物车、订单

//                .excludePathPatterns("/user/captcha")
//                .excludePathPatterns("/login/**")   //放行请求
//                .excludePathPatterns("/user/login/**")
//                .excludePathPatterns("/user/logout/**")
//                .excludePathPatterns("/static/**")
        ;

        //公共拦截器
        registry.addInterceptor(commonInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/login")
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/user/register");
    }


    /**
     * 放行静态资源
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //shop-manager 下所有静态资源路径前都要加上static
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
}
